package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//文章的增删改查
public class ArticleDao {
    //1.新增文章
    public void add(Article article){
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼接sql语句
        String sql = "insert into Article values (null,?,?,?)";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,article.getTitle());
            statement.setString(2,article.getContent());
            statement.setInt(3,article.getUserId());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret != 1){
                System.out.println("插入文章失败");
                return;
            }
            System.out.println("插入文章成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.释放连接
            DBUtil.close(connection,statement,null);
        }
    }

    //2.查看文章列表
    public List<Article> selectAll() {
        List<Article> articles = new ArrayList<>();
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼装sql语句
        String sql = "select articleId,title,userId from article";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            //3.执行sql
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                Article article = new Article();
                article.setArticleId(resultSet.getInt("articleId"));
                article.setTitle(resultSet.getString("title"));
                article.setUserId(resultSet.getInt("userId"));
                articles.add(article);
            }
            return articles;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //3.查看指定文章详情
    public Article selectById(int articleId){
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼装sql语句
        String sql = "select * from article where articleId = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,articleId);
            //3.执行sql语句
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                Article article = new Article();
                article.setArticleId(resultSet.getInt("articleId"));
                article.setTitle(resultSet.getString("title"));
                article.setContent(resultSet.getString("content"));
                article.setUserId(resultSet.getInt("userId"));
                return article;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.释放连接
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //4.删除指定文章(指定ID来删除)
    public void delete(int articleId){
        //1.建立连接
        Connection connection = DBUtil.getConnection();
        //2.拼装sql语句
        String sql = "delete from article where articleId = ?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,articleId);
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret != 1){
                System.out.println("删除文章操作失败");
                return;
            }
            System.out.println("删除文章操作成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.释放连接
            DBUtil.close(connection,statement,null);
        }
    }

    /*public static void main(String[] args) {
        ArticleDao articleDao = new ArticleDao();
        //1.测试add方法
       *//* Article article = new Article();
        article.setTitle("踏星");
        article.setContent("初一");
        article.setUserId(1);
        articleDao.add(article);*//*

        //2.测试selectAll()
        System.out.println(articleDao.selectAll());

        //3.测试selectById()
        System.out.println(articleDao.selectById(1));

        //4.测试delete()
        //articleDao.delete(1);
    }*/
}
